package com.godyao.mall.order.mq.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


/**
 * @author godyao
 * @date 2022/4/3
 */
@Configuration
public class RabbitMqConfig {
    /**
     * 交换机名称
     */
    public static final String EXCHANGE_NAME = "seckill_exchanger";

    /**
     * 队列名称
     */
    public static final String QUEUE_NAME = "seckill_queue";

    /**
     * 路由名称
     */
    public static final String ROUTE_KEY = "seckill_key";


    /**
     * 创建一个队列
     */
    @Bean(QUEUE_NAME)
    public Queue seckillQueue() {
        // 开启持久化
        return new Queue(QUEUE_NAME, true);
    }

    @Bean(EXCHANGE_NAME)
    public DirectExchange seckillExchange() {
        return new DirectExchange(EXCHANGE_NAME, true, false);
    }

    @Bean
    public Binding seckillBinding(@Qualifier(QUEUE_NAME) Queue queue, @Qualifier(EXCHANGE_NAME) Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(ROUTE_KEY).noargs();
    }


}
